﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using Gizmox.WebGUI.Common.Gateways;

namespace TVC
{
    public class DynamicStreamResourceHandle : Gizmox.WebGUI.Common.Resources.GatewayResourceHandle
    {
        public DynamicStreamResourceHandle(Stream argStream)
            : base(new GatewayReference(new StreamComponent(argStream, null), "SendStream"))
        {

        }

        public DynamicStreamResourceHandle(Stream argStream, String argContentType)
            : base(new GatewayReference(new StreamComponent(argStream, argContentType), "SendStream"))
        {
        }


        //Embedded Component - So we can access the HTTP Stream

        private class StreamComponent : Gizmox.WebGUI.Forms.Component
        {
            private Stream myStream;
            private String myContentType;

            public StreamComponent(Stream argStream, String argContentType)
            {
                myStream = argStream;
                myContentType = argContentType;
                //Register Myself with the Current VWG Application

                this.RegisterSelf();
            }

            protected override Gizmox.WebGUI.Common.Interfaces.IGatewayHandler ProcessGatewayRequest(System.Web.HttpContext objHttpContext, String strAction)
            {
                if (strAction == "SendStream")
                {
                    //Set ContentType
                    if (myContentType != null)
                    {
                        objHttpContext.Response.ContentType = myContentType;
                    }
                    //Disable Caching
                    objHttpContext.Response.Expires = -1;
                    //Send Stream to Browser
                    try
                    {
                        Byte[] myByteArray = GetStreamAsByteArray(myStream);
                        objHttpContext.Response.BinaryWrite(myByteArray);


                        //Close the HTTP Response Stream
                        //Used instead of objResponse.End()
                        //http://support.microsoft.com/kb/312629/

                        objHttpContext.Response.Flush();
                        objHttpContext.ApplicationInstance.CompleteRequest();

                        //Clean up
                        myStream = null;
                        myByteArray = null;
                    }
                    catch
                    {
                        return null;
                    }
                }

                return null;
            }

            private Byte[] GetStreamAsByteArray(System.IO.Stream stream)
            {
                try
                {
                    int streamLength = Convert.ToInt32(stream.Length);

                    Byte[] fileData = new Byte[streamLength];

                    //Read the Stream into a Byte array
                    stream.Position = 0;
                    stream.Read(fileData, 0, streamLength);
                    stream.Close();
                    return fileData;
                }
                catch
                {
                    return null;
                }
            }

            protected override void Dispose(bool disposing)
            {
                //Remove Myself from the Current VWG Application
                this.UnRegisterSelf();
                base.Dispose(disposing);
            }
        }
    }
}